//
//  HOTNotificationSettingViewController.m
//  Genpara
//
//  Created by AndyChang.zhang on 14-9-26.
//  Copyright (c) 2014年 zhangchh. All rights reserved.
//

#import "HOTNotificationSettingViewController.h"

@interface HOTNotificationSettingViewController () {
    
    NSMutableDictionary *_parameters;
}

@end

@implementation HOTNotificationSettingViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    [self setNaviTitle:@"プッシュ通知"];
    self.view.backgroundColor = [UIColor colorWithWhite:0.719 alpha:1.000];
    
    _parameters = [[NSMutableDictionary alloc] init];
    
    [_parameters setObject:self.userInfo.token forKey:REQ_KEY_USER_TOKEN];
    [_parameters setObject:self.userInfo.userId forKey:REQ_KEY_USER_ID];
    
    [self addRightBtnTarget:self action:@selector(doneBtnClicked) image:[UIImage imageNamed:@"btn_complete_off"] hImage:[UIImage imageNamed:@"btn_complete_on"]];
    
    [self requestDataFromNet];
}

-(void)requestDataFromNet {
    
    [self showLoading];
    
    //NSDictionary *parameters = @{REQ_KEY_USER_TOKEN:self.userInfo.token,
    //                             REQ_KEY_USER_ID:self.userInfo.userId};
    
    __unsafe_unretained HOTNotificationSettingViewController *tempObj = self;
    [self requestWithURL:HOT_URL_USER_PUSH Parameters:_parameters success:^(NSInteger result, NSDictionary * data) {
        
        [tempObj loadingSuccess];
        
        NSString *trigger = [data objectForKey:@"push_trigger"];
        if ([trigger isEqualToString:@"1"]) {
            
            [tempObj.pushBtn setOn:YES animated:NO];
        } else {
            [tempObj.pushBtn setOn:NO animated:NO];
        }
        
    } failure:^(NSError *error, NSString *errMsg) {
        
        [tempObj loadingSuccess];
        if ([tempObj checkErrResult:error errMsg:errMsg]) {
            return ;
        }
        if (errMsg.length > 0) {
            
            ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE
                                                                  message:errMsg
                                                                 delegate:nil
                                                        cancelButtonTitle:nil
                                                         otherButtonTitle:ALERT_BTN_OK];
            
            [popupView showInWindow];
        }
    }];
}

-(void)saveUpdate {
    
    
    [self showLoading];
    
    NSString *trigger = @"0";
    if (self.pushBtn.getValue) {
        
        trigger = @"1";
    }
    
    NSDictionary *parameters = @{REQ_KEY_USER_TOKEN:self.userInfo.token,
                                 REQ_KEY_USER_ID:self.userInfo.userId,
                                 REQ_KEY_PUSH_TRIGGER:trigger};
    
    __unsafe_unretained HOTNotificationSettingViewController *tempObj = self;
    [self requestWithURL:HOT_URL_USER_PUSH Parameters:parameters success:^(NSInteger result, NSDictionary * data) {
        
        [tempObj showSuccessWithMessage:MSG_SUCCESS complection:^{
            
            [tempObj.navigationController popViewControllerAnimated:YES];
        }];
        
    } failure:^(NSError *error, NSString *errMsg) {
        
        [tempObj loadingSuccess];
        if ([tempObj checkErrResult:error errMsg:errMsg]) {
            return ;
        }
        if (errMsg.length > 0) {
            
            ZZZPopupView *popupView = [[ZZZPopupView alloc] initWithTitle:ALERT_TITLE
                                                                  message:errMsg
                                                                 delegate:nil
                                                        cancelButtonTitle:nil
                                                         otherButtonTitle:ALERT_BTN_OK];
            
            [popupView showInWindow];
        }
    }];
}

-(void)doneBtnClicked {
    
    [self saveUpdate];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
